var that
const db = wx.cloud.database();
const _= db.command ;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topic: {},
    id: '',
    openid: '',
    pageLike: 0,
    follow:0,
    collection:0,
    like:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    that = this;
    that.data.id = options.id;
    that.data.openid = options.openid;
    that.data.count=Number(options.count);
    that.data.isLike=Number(options.isLike);
    that.setData({
      pageLike : that.data.isLike,
      count : that.data.count,
    })
    //保存访问次数
    db.collection('topic').doc(that.data.id)
    .update({
      data:{
        'count' : that.data.count
      }
    })
    // 获取话题信息
    db.collection('topic').doc(that.data.id).get({
      success: function(res) {
        that.topic = res.data;
        that.setData({
          topic: that.topic,
        })
      }
    })
    // 获取收藏情况
    db.collection('collect')
    .where({
        'c_id': that.data.id
      })
      .get({
        success: function(res) {
          console.log("判断是否收藏")
          if (res.data.length > 0) {
            console.log(res)
            that.data.collection = 1
            that.setData({
              collection : that.data.collection
            })
          } else {
            that.data.collection = 0
            that.setData({
              collection : that.data.collection
            })
          }
        },
        fail: console.error
      })
   // 获取关注情况
   db.collection('follow')
   .where({
       'f_id': that.data.openid
     })
     .get({
       success: function(res) {
         console.log("判断是否收藏")
         if (res.data.length > 0) {
           console.log(res)
           that.data.follow = 1
           that.setData({
             follow : that.data.follow
           })
         } else {
           that.data.follow = 0
           that.setData({
             follow : that.data.follow
           })
         }
       },
       fail: console.error
     })    
// 获取点赞情况
db.collection('like')
.where({
    'l_id': that.data.id
  })
  .get({
    success: function(res) {
      console.log("判断是否收藏")
      if (res.data.length > 0) {
        console.log(res)
        that.data.like = 1
        that.setData({
          like : that.data.like
        })
      } else {
        that.data.like = 0
        that.setData({
          like : that.data.like
        })
      }
    },
    fail: console.error
  })
  },

  onShow: function() {
    // 获取回复列表
    that.getReplay()
  },

  getReplay: function() {
    // 获取回复列表
    db.collection('replay')
      .where({
        t_id: that.data.id
      })
      .get({
        success: function(res) {
          // res.data 包含该记录的数据
          console.log(res)
          that.setData({
            replays: res.data
          })
        },
        fail: console.error
      })
  },
 
// 预览图片
previewImg: function(e) {
  //获取当前图片的下标
  var index = e.currentTarget.dataset.index;

  wx.previewImage({
    //当前显示图片
    current: this.data.topic.images[index],
    //所有图片
    urls: this.data.topic.images
  })
},
   /**
   * 点赞或取消点赞
   */
  onLikeClick: function(event) {
    if (that.data.like) {
      // 需要判断是否存在
      that.data.like = 0
      that.setData({
        like : that.data.like
      })
      that.removeFromLikeServer();
    } else {
      that.data.like = 1
      that.setData({
        like : that.data.like
      })
      that.saveToLikeServer();
    }
  },
  /**
   * 添加到点赞集合中
   */
  saveToLikeServer: function(event) {
   
    that.data.pageLike = that.data.pageLike+1;
    that.setData({
      pageLike : that.data.pageLike
    })
    //topic中点赞数量加一
    db.collection('topic').doc(that.data.id)
    .update({
      data:{
        'isLike' : that.data.pageLike
      },
      success:function(res){
        console.log("topic数据更改成功")
        db.collection('like').add({
          // data 字段表示需新增的 JSON 数据
          data: {
            l_id: that.data.id,
            date: new Date(),
          },
          success: function(e) {
            console.log(e)
          },
        })
      }
    })
    
   
  },
  /**
   * 从点赞集合中移除
   */
  removeFromLikeServer: function(event) {
    that.data.pageLike = that.data.pageLike-1;
    that.setData({
      pageLike : that.data.pageLike
    })
    //topic中点赞数量减一
    db.collection('topic').doc(that.data.id)
    .update({
      data:{
        'isLike' : that.data.pageLike
      },
      success:function(res){
        console.log("topic数据更改成功")
        //删除like集合记录
        db.collection('like').where({
          'l_id':that.data.id
        }).field({
          _id : true
        }).get({
          success:function(res){
            console.log(res.data[0]._id)
            db.collection('like').doc(res.data[0]._id)
            .remove({
              success:function(e){
                console.log("删除成功")
              }
            })
          }
        })
      }
    })
  
  },

  /**
   * 点击收藏或取消收藏
   */
  onCollectClick: function(event) {
    if (that.data.collection) {
      // 需要判断是否存在
      that.data.collection = 0
      that.setData({
        collection : that.data.collection
      })
      that.removeFromCollectServer();
    } else {
      that.data.collection = 1
      that.setData({
        collection : that.data.collection
      })
      that.saveToCollectServer();
    }
  },
  /**
   * 添加到收藏集合中
   */
  saveToCollectServer: function(event) {
    db.collection('topic').doc(that.data.id)
    .update({
      data:{
        collect : _.inc(1)
      },
      success:function(e){
        db.collection('collect').add({
          // data 字段表示需新增的 JSON 数据
          data: {
            c_id: that.data.id,
            date: new Date(),
          },
          success: function(res) {
            console.log(res)
          },
        })
      }
    })
   
  },
  /**
   * 从收藏集合中移除
   */
  removeFromCollectServer: function(event) {
    db.collection('topic').doc(that.data.id)
    .update({
      data:{
        collect : _.inc(-1)
      },
      success:function(event){
        db.collection('collect').where({
          'c_id':that.data.id
        }).field({
          _id : true
        }).get({
          success:function(res){
            console.log(res.data[0]._id)
            db.collection('collect').doc(res.data[0]._id)
            .remove({
              success:function(e){
                console.log("删除成功")
              }
            })
          }
        })
      }
    })
   
    
  },
/**
   * 点击关注
   */
  onFollowClick: function(event) {
    if (that.data.follow) {
      // 需要判断是否存在
      that.data.follow = 0
      that.setData({
        follow : that.data.follow
      })
      that.removeFromFollowServer();
    } else {
      that.data.follow = 1
      that.setData({
        follow : that.data.follow
      })
      that.saveToFollowServer();
    }
  },
  /**
   * 添加到关注集合中
   */
  saveToFollowServer: function(event) {
    db.collection('follow').add({
      // data 字段表示需新增的 JSON 数据
      data: {
        f_id: that.data.openid,
        date: new Date(),
      },
      success: function(res) {
        console.log(res)
        //更新粉丝数量
        db.collection('fans').where({
          _id : that.data.openid
        }).get({
          success:function(res){
            console.log(res)
            if(res.data.length > 0){
              db.collection('fans').doc(that.data.openid)
              .update({
                data:{
                  fans : _.inc(1)
                }
              })
            }else{
              db.collection('fans').add({
                data:{
                  _id :that.data.openid,
                  fans:1
                },
                success:function(res){
                  console.log("fans添加新的数据")
                }
              })
            }
          }
        })
      },
    })
  },
  /**
   * 从收藏集合中移除
   */
  removeFromFollowServer: function(event) {
    db.collection('follow').where({
      'f_id':that.data.openid
    }).field({
      _id : true
    }).get({
      success:function(res){
        console.log(res.data[0]._id)
        db.collection('follow').doc(res.data[0]._id)
        .remove({
          success:function(e){
            console.log("删除成功")
            //粉丝数量减一
            db.collection('fans').doc(that.data.openid)
            .update({
              data:{
                fans : _.inc(-1)
              }
            })
          }
        })
      }
    })
  },
  
  /**
   * 跳转回复页面
   */
  onReplayClick() {
    wx.navigateTo({
      url: "../replay/replay?id=" + that.data.id + "&openid=" + that.data.openid
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})